package com.dongnam.quanly.common.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dongnam.quanly.common.model.VatTuHangHoa;

public class VatTuHangHoaDao extends ExtendedGenericDao<VatTuHangHoa, String> {

	private static final Logger logger = LoggerFactory.getLogger(VatTuHangHoaDao.class);

	public long findCountByFilters() {
		Criteria crit = getSession().createCriteria(getPersistentClass());
		return ((Number) crit.setProjection(Projections.rowCount()).uniqueResult()).longValue();
	}

	@SuppressWarnings("unchecked")
	public List<VatTuHangHoa> findByFilters(int pageSize, int pageNumber, String sortOrder,String sortField,Map<String, Object> filters) {

		Criteria crit = getSession().createCriteria(getPersistentClass());

		for(String key : filters.keySet())
		{
			crit.add(Restrictions.like(key, (String)filters.get(key), MatchMode.ANYWHERE));
		}
		
		
		crit.setFirstResult(pageNumber * pageSize);
		crit.setMaxResults(pageSize);

		crit.setFirstResult(pageNumber * pageSize);
		crit.setMaxResults(pageSize);
		if (sortOrder != null && !sortOrder.equalsIgnoreCase("UNSORTED") && sortField != null) {
			if (sortOrder.equalsIgnoreCase("ASCENDING")) {
				crit.addOrder(Order.asc(sortField));
			} else {
				crit.addOrder(Order.desc(sortField));
			}
			
			
		}
		return crit.list();
	}

}
